<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          v-hasPermi="['info:blacklist:add']"
        >刷新
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['info:blacklist:add']"
        >设备指令下发
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['info:blacklist:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blacklistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="车牌号" align="center" prop="licenseNumber"/>
      <el-table-column label="物业名称" align="center" prop="propertyManagement.propertyName"/>
      <el-table-column label="记录设备名称" align="center" prop="carsHomeEqIdentify.entifyname"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="停车场" align="center" prop="carsHome.chname"/>
      <el-table-column label="结束时间" align="center" prop="overTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.overTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="0 黑 1 白" align="center" prop="carsState">
        <template slot-scope="scope">
          <button class="white-list-button">{{ scope.row.carsState === 0 ? '黑名单' : '白名单' }}</button>
        </template>
      </el-table-column>
      <el-table-column label="下发状态">
        <button class="white-list-button">已下发</button>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改车辆黑名单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车牌号" prop="licenseNumber">
          <el-input v-model="form.licenseNumber" placeholder="请输入车牌号"/>
        </el-form-item>
        <el-form-item label="物业名称" >
          <el-select v-model="form.pmid" @visible-change="prosList1" clearable>
            <el-option v-for="item in this.properList1" :key="item.pmid" :label="item.propertyName"
                       :value="item.pmid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车场名称" >
          <el-select v-model="form.chid" @visible-change="carsList1" clearable>
            <el-option v-for="item in this.carList1" :key="item.chid" :label="item.chname"
                       :value="item.chid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="记录设备名称" prop="entifyid">
          <el-input v-model="form.entifyid" placeholder="请输入记录设备名称"/>
        </el-form-item>
        <el-form-item label="指令执行时间">
          <el-date-picker
            type="datetimerange"
            is-range
            v-model="formTime"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="handleChangeDateInEvent"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作类型">
          <el-select v-model="addExa" disabled>
            <el-option label="新增" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="黑白名单下发" prop="carsState">
        <el-select  v-model="form.carsState">
          <el-option label="黑名单" :value="0" />
          <el-option label="白名单" :value="1" />
        </el-select>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listBlacklist, getBlacklist, delBlacklist, addBlacklist, updateBlacklist} from "@/api/info/blacklist";
import Operation from "@/views/pxy-cars/cars/operation.vue";
import {listHome, listProperty} from "@/api/ruoyi-home/home";

export default {
  name: "Blacklist",
  components: {Operation},
  data() {
    return {
      addExa: 0,
      properList1: [],
      carList1: [],
      formTime: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆黑名单表格数据
      blacklistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        licenseNumber: null,
        pmid: null,
        entifyid: null,
        startTime: null,
        chid: null,
        overTime: null,
        carsState: null
      },
      proId: {
        pmid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          {required: true, message: "唯一编码,前台使用不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        deleted: [
          {required: true, message: "是否删除不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "修改时间不能为空", trigger: "blur"}
        ],
        licenseNumber: [
          {required: true, message: "车牌号不能为空", trigger: "blur"}
        ],
        pmid: [
          {required: true, message: "所停小区名称不能为空", trigger: "blur"}
        ],
        entifyid: [
          {required: true, message: "记录设备id不能为空", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "开始时间不能为空", trigger: "blur"}
        ],
        chid: [
          {required: true, message: "车场id不能为空", trigger: "blur"}
        ],
        overTime: [
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ],
        carsState: [
          {required: true, message: "0 黑 1 白不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    prosList1()
    {
      listProperty().then(res => {
        this.properList1 = res.rows;
        this.form.chid = null;
      })
    },
    carsList1()
    {
      this.proId.pmid = this.form.pmid;
      listHome(this.proId).then(res => {
        this.carList1 = res.rows
      })
    },
    /** 查询车辆黑名单列表 */
    getList() {
      this.loading = true;
      listBlacklist(this.queryParams).then(response => {
        // console.log(response.rows)
        this.blacklistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cbid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        licenseNumber: null,
        pmid: null,
        entifyid: null,
        startTime: null,
        chid: null,
        overTime: null,
        carsState: null
      };
      this.resetForm("form");
    },
    handleChangeDateInEvent() {
      this.form.startTime = this.formTime[0];
      this.form.overTime = this.formTime[1];
      // console.log(this.form)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cbid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆黑名单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cbid = row.cbid || this.ids
      getBlacklist(cbid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆黑名单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cbid != null) {
            updateBlacklist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBlacklist(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cbids = row.cbid || this.ids;
      this.$modal.confirm('是否确认删除车辆黑名单编号为"' + cbids + '"的数据项？').then(function () {
        return delBlacklist(cbids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sys-cars-info/blacklist/export', {
        ...this.queryParams
      }, `blacklist_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.white-list-button {
  background-color: #ebf4fe; /* 按钮颜色 */
  border: none;
  color: #779efe;
  padding: 6px 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 12px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 8px; /* 按钮圆角 */
}

.white-list-button:hover {
  background-color: #ebf4fe;
}
</style>
